﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace _4mvcBlog.Core.ViewEngine
{
    public class ThemeViewEngine : VirtualPathProviderViewEngine
    {
        private string _theme;
        /// <summary>
        /// 使用的主题名称
        /// </summary>
        public string Theme
        {
            get { return _theme; }
            set
            {
                _theme = string.IsNullOrEmpty(value) ? "Default" : value;
                SetLocationFormats(_theme);
            }
        }

        public ThemeViewEngine(string themeName)
        {
            Theme = themeName;
        }

        /// <summary>
        /// 设置视图的搜索路径集合
        /// </summary>
        /// <param name="themeName">主题名</param>
        private void SetLocationFormats(string themeName)
        {
            base.MasterLocationFormats = new[] {
                "~/Views/Themes/" + themeName + "/{1}/{0}.master",
                "~/Views/Themes/" + themeName + "/Shared/{0}.master",
                "~/Views/Themes/Shared/{0}.master"
            };

            base.ViewLocationFormats = new[] {
                "~/Views/Themes/" + themeName + "/{1}/{0}.aspx",
                "~/Views/Themes/" + themeName + "/{1}/{0}.ascx",
                "~/Views/Themes/" + themeName + "/Shared/{0}.aspx",
                "~/Views/Themes/" + themeName + "/Shared/{0}.ascx",
                "~/Views/Themes/Shared/{1}/{0}.aspx",
                "~/Views/Themes/Shared/{1}/{0}.ascx",
                "~/Views/Themes/Shared/{0}.aspx",
                "~/Views/Themes/Shared/{0}.ascx"
            };

            base.PartialViewLocationFormats = base.ViewLocationFormats;
        }

        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath) {
            return new WebFormView(partialPath);
        }

        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath) {
            return new WebFormView(viewPath, masterPath);
        }
    }
}
